/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator.gui.panel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.googlecode.armycreator.domain.Army;
import com.googlecode.armycreator.domain.GameSystem;
import com.googlecode.armycreator.domain.IGameSystem;
import com.googlecode.armycreator.domain.comparator.ArmyNameComparator;
import com.googlecode.armycreator.domain.comparator.GameSystemNameComparator;
import com.googlecode.armycreator.dto.RosterDto;
import com.googlecode.armycreator.gui.presenter.RosterPresenter;

public class CreateNewRosterPanel extends Panel {

    private RosterDto rosterDto;
    private RosterPresenter rosterPresenter;

    public CreateNewRosterPanel(String id, final RosterDto rosterDto, Set<GameSystem> availableSystems,
            RosterPresenter rosterPresenter) {
        super(id, new CompoundPropertyModel<RosterDto>(rosterDto));
        this.rosterDto = rosterDto;
        this.rosterPresenter = rosterPresenter;
        setOutputMarkupId(true);

        add(CSSPackageResource.getHeaderContribution(getClass(), "CreateNewRosterPanel.css"));

        Form<RosterDto> form = new CreateNewRosterForm("createNewRosterForm", rosterDto, availableSystems);

        add(form);
        add(new FeedbackPanel("feedback"));
    }

    private static final long serialVersionUID = 7987626300190958667L;

    class CreateNewRosterForm extends Form<RosterDto> {

        @Override
        protected void onSubmit() {
            rosterPresenter.newRosterCreated(rosterDto);
        }

        public CreateNewRosterForm(String id, RosterDto rosterDto, Set<GameSystem> availableSystems) {
            super(id);

            add(new RequiredTextField<RosterDto>("name").setLabel(new Model<String>("Name:")).setOutputMarkupId(true));

            ArmyDropDownChoice armyDropDownChoice = new ArmyDropDownChoice("army", rosterDto);
            armyDropDownChoice.setRequired(true).setLabel(new Model<String>("Army:")).setOutputMarkupId(true);

            GameSystemDropDownChoice gameSystemDropDownChoice = new GameSystemDropDownChoice("gameSystem",
                    availableSystems, armyDropDownChoice);
            gameSystemDropDownChoice.setRequired(true).setLabel(new Model<String>("Game System:")).setOutputMarkupId(
                    true);

            add(gameSystemDropDownChoice);
            add(armyDropDownChoice);
            add(new TextField<RosterDto>("pointsLimit").setLabel(new Model<String>("Points Limit:")).setOutputMarkupId(
                    true));
            add(new Button("submit", new Model<String>("Create")).setOutputMarkupId(true));
        }

        private static final long serialVersionUID = 2180610445535599421L;
    }

    class ArmyDropDownChoice extends DropDownChoice<Army> {

        public ArmyDropDownChoice(String id, final RosterDto rosterDto) {
            super(id);

            IModel<List<? extends Army>> armies = new AbstractReadOnlyModel<List<? extends Army>>() {
                @Override
                public List<Army> getObject() {
                    IGameSystem gameSystem = rosterDto.getGameSystem();
                    List<Army> armies = null;

                    if (gameSystem != null) {
                        armies = new ArrayList<Army>(gameSystem.exportAsBean().getArmies());
                        Collections.sort(armies, new ArmyNameComparator());
                    }

                    if (armies == null) {
                        armies = Collections.emptyList();
                    }

                    return armies;
                }

                private static final long serialVersionUID = 1L;
            };

            setChoices(armies);
            setChoiceRenderer(new IChoiceRenderer<Army>() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object getDisplayValue(Army army) {
                    return army.exportAsBean().getName();
                }

                @Override
                public String getIdValue(Army army, int index) {
                    return Integer.toString(index);
                }
            });
        }

        private static final long serialVersionUID = 6253352629287898965L;
    }

    class GameSystemDropDownChoice extends DropDownChoice<GameSystem> {

        public GameSystemDropDownChoice(String id, Set<GameSystem> availableSystems,
                final ArmyDropDownChoice armyDropDownChoice) {
            super(id);
            ArrayList<GameSystem> choices = new ArrayList<GameSystem>(availableSystems);
            Collections.sort(choices, new GameSystemNameComparator());
            setChoices(choices);

            add(new AjaxFormComponentUpdatingBehavior("onchange") {

                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    target.addComponent(armyDropDownChoice);
                }

                private static final long serialVersionUID = 1L;
            });

            setChoiceRenderer(new IChoiceRenderer<GameSystem>() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object getDisplayValue(GameSystem gameSystem) {
                    return gameSystem.exportAsBean().getName();
                }

                @Override
                public String getIdValue(GameSystem gameSystem, int index) {
                    return Integer.toString(index);
                }
            });
        }

        private static final long serialVersionUID = 2139620858681746594L;
    }
}
